package com.deyuanyun.pic.pipe.controller;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.StaticParam;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ReadExcel;
import com.deyuanyun.pic.pipe.domain.exceldomain.CelbowImportDemo;
import com.deyuanyun.pic.pipe.domain.exceldomain.ElbowImportDemo;
import com.deyuanyun.pic.pipe.service.PipeElbowService;
import com.deyuanyun.pic.support.utils.ExcelUtils;
import com.github.pagehelper.Page;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 弯头数据录入
 * 
 * 
 * @author scalpel
 * @version 1.0
 * @date 2016年1月26日下午2:21:15
 * Method List:                       // 主要函数及其功能
 *             1.---------------- 
 *             search 搜索弯头数据
 *             importData2 导出热煨玩图
 *             importData  导入冷弯弯头
 *             del         导出热煨弯头
 *             del2		        导出冷弯弯头
 *             
 *History:                           // 历史修改记录
 */
@Controller
@RequestMapping("/pipe/elbow")
public class PipeElBowController extends PipeBaseController{

	@Resource
	private PipeElbowService pipeElbowService;
	
//	@RequestMapping("/page.html")
//	public ModelAndView page(HttpServletRequest request){
//		ModelAndView mdv=new ModelAndView();
//		
//		
//		return mdv;
//	}
//	
	@RequestMapping("/search.json")
	@ResponseBody
	public void search(HttpServletRequest request){
		
		HashMap map=RequestUtil.parseRequestParamToHashMap(request);
		PageJsonBean jsonBean=new PageJsonBean();
		Page main=pipeElbowService.findPage(map);
		
		jsonBean.setList(main);
		AjaxSupport.sendSuccessText("ok", jsonBean);
		
	}
	@RequestMapping("/import.json")
	@ResponseBody
	public void importData(@RequestParam MultipartFile file,HttpServletResponse response) throws IOException, Exception{
		
		List<CelbowImportDemo> data=ReadExcel.readExcle(CelbowImportDemo.class,file.getInputStream(),4,"17");
		data= MyCollectionUtil.removeLastEmpty(data);
		VResult b= BeanValidateUtil.vali2(data,4+"");
		if (b.isRelult()){
			pipeElbowService.importCElbowData(data);
			 AjaxResponse.sendText(response, StaticParam.UPLOAD_FILE_SUCCESS);
		}else{
			 String[] titleArr= ReadExcel.getTitle(file,"in/celbow_t.xls");
	            String m=b.getAllString(titleArr);
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
		}
		
	}
	
	@RequestMapping("/import2.json")
	@ResponseBody
	public void importData2(@RequestParam MultipartFile file,HttpServletResponse response) throws IOException, Exception{
		
		List<ElbowImportDemo> data=ReadExcel.readExcle(ElbowImportDemo.class,file.getInputStream(),4,"22");
		data= MyCollectionUtil.removeLastEmpty(data);
		VResult b= BeanValidateUtil.vali2(data,4+"");
		if (b.isRelult()){
			pipeElbowService.importElbowData(data);
			 AjaxResponse.sendText(response,StaticParam.UPLOAD_FILE_SUCCESS);
		}else{
			String[] titleArr= ReadExcel.getTitle(file,"in/elbow_t.xls");
            String m=b.getAllString(titleArr);
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
		}
		
	}
	
	@RequestMapping("/del.json")
	@ResponseBody
	public void del(HttpServletRequest request){
		String id=request.getParameter("id");
		if(!StringUtils.isEmpty(id))
		{
			HashMap data=RequestUtil.parseRequestParamToHashMap(request);
			int dbresult=pipeElbowService.delCElbowData(data);
			Map<String,String> result=new HashMap<String,String>();
			result.put("status", "0");
			AjaxSupport.sendSuccessText("ok", result);
		}else{
			Map<String,String> result=new HashMap<String,String>();
			result.put("status", "1");
			AjaxSupport.sendSuccessText("ok", result);
		}
	}
	
	@RequestMapping("/del2.json")
	@ResponseBody
	public void del2(HttpServletRequest request){
		String id=request.getParameter("id");
		if(!StringUtils.isEmpty(id))
		{
			HashMap data=RequestUtil.parseRequestParamToHashMap(request);
			pipeElbowService.delElbowData(data);
			Map<String,String> result=new HashMap<String,String>();
			result.put("status", "0");
			AjaxSupport.sendSuccessText("ok", result);
		}else{
			Map<String,String> result=new HashMap<String,String>();
			result.put("status", "1");
			AjaxSupport.sendSuccessText("ok", result);
		}
	}
	
	@RequestMapping("/excelExport.json")
	@ResponseBody
	public ModelAndView excelExport(HttpServletResponse response,
			HttpServletRequest request) {
		HashMap data=RequestUtil.parseRequestParamToHashMap(request);
		InputStream filePath = this.getClass().getResourceAsStream("/template/out/elbow_template.xls");
		List<List<String[]>> result = pipeElbowService.excelExport(data);
		// 将Object中存的List取出
		int[] lineStart = new int[] { 2,1 };
		ExcelUtils.jxlExportExcel(filePath, lineStart, "弯头", result, response);

		return null;
	}
}
